package cn.edu.scau.cmi.zhangjunming.composite.consisentPersion;

import java.net.URL;
import java.util.Iterator;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class ConsistentController implements Initializable{

	private static Stage stage = null;
	private static Scene scene = null;
	@FXML Button deleteButton;
	@FXML Button pButton;
	@FXML Button returnButton;
	@FXML Button showButton;
	@FXML Button gButton;
	@FXML Button upButton;
	@FXML Button downButton;
	@FXML TextArea showArea;
	@FXML TextField inArea;
	@FXML ComboBox nodeBox;
	
	ConsistentComponent trunk;
	ConsistentComponent way;
	//ConsistentComponent
	
	public ConsistentController() {
		// TODO 自动生成的构造函数存根
	}

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		
		trunk = new ConsistentComposite();
		((ConsistentComposite)trunk).setName("总公司");
		
		ConsistentComponent firstBranch = new ConsistentComposite();
		((ConsistentComposite)firstBranch).setName("团队1");
		
		ConsistentComponent secondBranch = new ConsistentComposite();
		((ConsistentComposite)secondBranch).setName("团队2");
		
		ConsistentLeaf firstLeaf = new ConsistentLeaf();
		((ConsistentLeaf)firstLeaf).setName("工具人1-1");
		
		ConsistentLeaf secondLeaf = new ConsistentLeaf();
		((ConsistentLeaf)secondLeaf).setName("工具人1-2");
		
		ConsistentLeaf thirdLeaf = new ConsistentLeaf();
		((ConsistentLeaf)thirdLeaf).setName("工具人2-1");
		
		ConsistentLeaf forthLeaf = new ConsistentLeaf();
		((ConsistentLeaf)forthLeaf).setName("工具人2-2");
		
		firstBranch.addChild(firstLeaf);
		firstBranch.addChild(secondLeaf);
		secondBranch.addChild(thirdLeaf);
		secondBranch.addChild(forthLeaf);
		
		trunk.addChild(firstBranch);
		trunk.addChild(secondBranch);
		
		firstBranch.setParent(trunk);
		secondBranch.setParent(trunk);
		
		firstLeaf.setParent(firstBranch);
		secondLeaf.setParent(firstBranch);
		thirdLeaf.setParent(secondBranch);
		forthLeaf.setParent(secondBranch);
		
		way = trunk;
		
		refreshBox();
		
	}
	
	@FXML private void pathUp() {
		if(way.getParent()!=null) {
			way = way.getParent();
			refreshBox();
		}
	}
	
	@FXML private void pathDown() {
		int index = nodeBox.getSelectionModel().getSelectedIndex();
		Iterator<ConsistentComponent> tool = way.getChild().iterator();
		for(int i=0;i<index;i++) {
			tool.next();
		}
		ConsistentComponent node = tool.next();
		if(node.type==0) {
			way = node;
			refreshBox();
		}
		else {
			showArea.clear();
			showArea.appendText("该节点不是团体，无法打开");
		}
	}
	
	@FXML private void get_return() {
		Stage stage = (Stage)returnButton.getScene().getWindow();
		stage.close();
	}
	
	private void refreshBox() {
		nodeBox.getItems().remove(0,nodeBox.getItems().size());
		Iterator<ConsistentComponent> tool = way.getChild().iterator();
		for(int i=0;i<way.getChild().size();i++) {
			ConsistentComponent node = tool.next();
			if(node.type==0) {
				ConsistentComposite node1 = (ConsistentComposite)node;
				nodeBox.getItems().add(new String("[Team]"+node1.getName()));
			}
			else {
				ConsistentLeaf node2 = (ConsistentLeaf)node;
				nodeBox.getItems().add(new String("[Person]"+node2.getName()));
			}
		}
	}
	
	@FXML private void delete_node() {
		int index = nodeBox.getSelectionModel().getSelectedIndex();
		Iterator<ConsistentComponent> tool = way.getChild().iterator();
		for(int i=0;i<index;i++) {
			tool.next();
		}
		ConsistentComponent node = tool.next();
		ConsistentComposite node2 = (ConsistentComposite)way;
		node2.deliete(node);
		showArea.clear();
		showArea.setText(new String("结点已清除！"+"\n"));
	}
	
	@FXML private void addPerson() {
		String name = inArea.getText();
		ConsistentLeaf newPerson = new ConsistentLeaf();
		newPerson.setName(name);
		newPerson.setParent(way);
		way.addChild(newPerson);
		refreshBox();
	}
	
	@FXML private void addGroup() {
		String name = inArea.getText();
		ConsistentComposite newGroup = new ConsistentComposite();
		newGroup.setName(name);
		newGroup.setParent(way);
		way.addChild(newGroup);
		refreshBox();
	}
	
	@FXML private void show_tree() {
		showArea.clear();
		showConstruction((ConsistentComposite)way,1);
	}
	
	private void showConstruction(ConsistentComposite root,int depth) {
		for(int i=0;i<depth;i++) {
			showArea.appendText("    ");
		}
		showArea.appendText("[Team]"+root.getName()+"\n");
		Iterator<ConsistentComponent> tool = root.getChild().iterator();
		for(int i=0;i<root.getChild().size();i++) {
			ConsistentComponent node  = tool.next();
			if(node.type==0) {
				ConsistentComposite node1 = (ConsistentComposite) node;
				showConstruction(node1,depth+1);
			}
			else {
				ConsistentLeaf node2 = (ConsistentLeaf) node;
				for(int j=0;j<depth+1;j++) {
					showArea.appendText("    ");
				}
				showArea.appendText("[Person]"+node2.getName()+"\n");
			}
		}
	}

	public static void setStage(Stage stage) {
		ConsistentController.stage = stage;
	}
	
	public static Stage getStage() {
		return stage;
	}
}
